package thread.shengchanxiaofei;


import java.io.File;
import java.util.concurrent.BlockingQueue;

public class Crawler implements Runnable {
    private final BlockingQueue<File> b;
    private final File root;

    @Override
    public void run() {
        System.out.println("Crawler begins to run!");
        if (root == null|| !root.exists()) {
            System.out.println("file不存在或者为null");
            return;
        }
        crawl(root);
        System.out.println("Crawler is shutdown!");

    }

    public void crawl(File root) {
        try {
            if (root.isFile()) {
                System.out.println("Crawling " + root);
                b.put(root);
            } else {
                for (File f : root.listFiles()) {
                    crawl(f);
                }
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }

    public Crawler(BlockingQueue<File> b, File root) {
        this.b = b;
        this.root = root;
    }

}